package com.lagou.sqlSession;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;

import java.lang.reflect.*;
import java.util.List;

/**
 * @author DingYiYuan
 * @description
 * @time 2021/3/10 16:00
 */
public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... params) throws Exception {

        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        //将方法交给Executor执行
        List<Object> list = simpleExecutor.query(configuration, mappedStatement, params);

        return (List<E>) list;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws Exception {
        List<Object> objects = selectList(statementId, params);
        if (objects.size() == 1) {
            return (T) objects.get(0);
        } else {
            throw new RuntimeException("查询结果为空或者返回结果过多");
        }
    }

    @Override
    public <E> List<E> insertOne(String statementId, Object... params) throws Exception {

        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        return simpleExecutor.query(configuration, mappedStatement, params);
    }

    @Override
    public <E> List<E> updateOne(String statementId, Object... params) throws Exception {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        return simpleExecutor.query(configuration, mappedStatement, params);
    }

    @Override
    public <E> List<E> deleteOne(String statementId, Object... params) throws Exception {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        return simpleExecutor.query(configuration, mappedStatement, params);
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        //使用JDK动态代理来为Dao接口生成代理对象，并返回
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(),
                new Class[]{mapperClass}, (Object proxy, Method method, Object[] args) -> {

                    //方法名
                    String methodName = method.getName();
                    //类名
                    String className = method.getDeclaringClass().getName();
                    //类名+方法名组成statementId
                    String statementId = className + "." + methodName;

                    //获取被调用方法的返回值类型
                    Type genericReturnType = method.getGenericReturnType();
                    //判断是否进行了 泛型类型参数化
                    if (genericReturnType instanceof ParameterizedType) {
                        if(methodName.equals("selectList")){
                            List<Object> objects = selectList(statementId, args);
                            return objects;
                        }
                    }
                    if(methodName.equals("selectOne")){
                        return selectOne(statementId, args);
                    }
                    if(methodName.equals("insertOne")){
                        return insertOne(statementId, args);
                    }
                    if(methodName.equals("updateOne")){
                        return updateOne(statementId, args);
                    }
                    return deleteOne(statementId, args);
                }
        );
        return (T) proxyInstance;
    }


}
